/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.ChiTietChuongTrinhKhuyenMaiDAO;
import dao.ChiTietSanPhamDAO;
import dao.ChuongTrinhKhuyenMaiDAO;
import dao.CuaHangDAO;
import dao.LoaiNhanVienDAO;
import dao.LoaiSanPhamDao;
import dao.SanPhamDao;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Chitietchuongtrinhkm;
import pojo.Chitietsanpham;
import pojo.Chuongtrinhkhuyenmai;
import pojo.Cuahang;
import pojo.Loainv;
import pojo.Loaisp;
import pojo.Sanpham;

/**
 *
 * @author sangnguyen
 */
public class JDialogChiTietChuongTrinhKM extends javax.swing.JDialog {

    /**
     * Creates new form JDialogChiTietChuongTrinhKM
     */
    private DefaultComboBoxModel cmbModelCH;
    private DefaultComboBoxModel cmbModelLoaiSP;
    private DefaultTableModel tbModel;
    private DefaultTableModel tbModelKM;
    private List<Sanpham> ls;    
    private List<Chitietchuongtrinhkm> lsChitietKM;
    private Vector vtMaLoaiCH;
    private Vector vtTenCH;
    private Vector vtMaLoaiSP;
    private Vector vtLoaiSP;
    private int maCH;
    private int maLoaiSP;        
    public JDialogChiTietChuongTrinhKM(java.awt.Frame parent, boolean modal) {
        
        super(parent, modal);
        initComponents();   
        //JInternalFrameChuongTrinhKhuyenMai frmCTKM = new JInternalFrameChuongTrinhKhuyenMai();                         
        System.out.println(JInternalFrameChuongTrinhKhuyenMai.getTenCTKM());        
        this.jLabelTenCTKM.setText(JInternalFrameChuongTrinhKhuyenMai.getTenCTKM());
        this.jLabelNgayBD.setText(JInternalFrameChuongTrinhKhuyenMai.getNgayBD());
        this.jLabelNgayKT.setText(JInternalFrameChuongTrinhKhuyenMai.getngayKT());
        vtMaLoaiCH = new Vector();
        vtTenCH = new Vector();
        List<Cuahang> dsCH= CuaHangDAO.SelectAllCuaHang();
        if(dsCH.size()!=0){                    
            for(int i=0;i<dsCH.size();i++){
                vtMaLoaiCH.add(dsCH.get(i).getIdch());
                vtTenCH.add(dsCH.get(i).getTench());
            }
        }
        cmbModelCH = new DefaultComboBoxModel(vtTenCH);
        this.jComboBoxCuaHang.setModel(cmbModelCH);
        
        //LoaiSP
        vtMaLoaiSP = new Vector();
        vtLoaiSP = new Vector();
        List<Loaisp> dsLoaiSP= LoaiSanPhamDao.SelectAllLoaisp();
        if(dsLoaiSP.size() !=0)
        {
            for(int i=0;i<dsCH.size();i++){
            vtMaLoaiSP.add(dsLoaiSP.get(i).getIdloai());
            vtLoaiSP.add(dsLoaiSP.get(i).getTenloai());
            }
        }        
        cmbModelLoaiSP = new DefaultComboBoxModel(vtLoaiSP);
        this.jComboBoxLoaiSP.setModel(cmbModelLoaiSP); 
        int idCTKM = JInternalFrameChuongTrinhKhuyenMai.getIDCTKM();
        lsChitietKM = ChiTietChuongTrinhKhuyenMaiDAO.SelectAllChitietchuongtrinhkm(idCTKM);
        this.tbModelKM = new DefaultTableModel(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; 
        }
        };
        this.tbModelKM.addColumn("STT");        
        this.tbModelKM.addColumn("Tên sản phẩm");                
        this.tbModelKM.addColumn("Đơn giá nhập");  
        this.tbModelKM.addColumn("Đơn giá bán");  
        this.tbModelKM.addColumn("Số lượng tồn");                                
        this.tbModelKM.addColumn("Cửa hàng");
        this.tbModelKM.addColumn("Hệ số giảm");
        System.out.println(lsChitietKM.size());
        for(int i=0;i<lsChitietKM.size();i++)
        {
            Vector vt = new Vector();            
            vt.add(i+1);
            Chitietsanpham objCTSP = new Chitietsanpham();
            objCTSP = lsChitietKM.get(i).getChitietsanpham(); 
            Cuahang objCH = new Cuahang();
            objCH = lsChitietKM.get(i).getChitietsanpham().getCuahang();
            vt.add(objCTSP.getSanpham().getTensp());
            vt.add(objCTSP.getSanpham().getDongianhap()); 
            vt.add(objCTSP.getGiaban());
            vt.add(objCTSP.getSanpham().getSoluongton());            
            vt.add(objCH.getTench());
            vt.add(lsChitietKM.get(i).getHesogiamgia());
            tbModelKM.addRow(vt);
        }        
        this.jTableCTKM.setModel(tbModelKM);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabelTenCTKM = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabelNgayBD = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabelNgayKT = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxCuaHang = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxLoaiSP = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDanhSachSP = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabelTenSP = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabelGiaNhap = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButtonApdung = new javax.swing.JButton();
        jButtonHuy = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabelGiaBan = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableCTKM = new javax.swing.JTable();
        jTextFieldHeSo = new javax.swing.JTextField();
        jButtonTimKiem = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Chi Tiết Chương Trình Khuyến Mãi");
        setModal(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Chi Tiết Chương Trình Khuyến Mãi");

        jLabelTenCTKM.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelTenCTKM.setText("ten");

        jLabel3.setText("Ngày bắt đầu");

        jLabelNgayBD.setText("ngayBD");

        jLabel4.setText("Ngày kết thúc");

        jLabelNgayKT.setText("ngayKT");

        jLabel2.setText("Cửa hàng");

        jLabel5.setText("Loại sản phẩm");

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));

        jTableDanhSachSP.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableDanhSachSP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDanhSachSPMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableDanhSachSP);

        jLabel6.setText("Danh sách sản phẩm");

        jLabel7.setText("Tên sản phẩm");

        jLabelTenSP.setText("ten san pham");

        jLabel8.setText("Giá nhập");

        jLabelGiaNhap.setText("Gia Nhap");

        jLabel9.setText("Hệ số giảm");

        jButtonApdung.setText("Áp dụng");
        jButtonApdung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonApdungActionPerformed(evt);
            }
        });

        jButtonHuy.setText("Hủy");
        jButtonHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHuyActionPerformed(evt);
            }
        });

        jLabel11.setText("%");

        jLabel12.setText("Giá bán");

        jLabelGiaBan.setText("giaBan");

        jLabel10.setText("Danh sách sản phẩm khuyến mãi");

        jTableCTKM.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableCTKM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableCTKMMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableCTKM);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(495, 495, 495)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel9)
                                            .addComponent(jLabel12)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(487, 487, 487)
                                        .addComponent(jButtonApdung, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabelGiaNhap)
                                    .addComponent(jLabelTenSP)
                                    .addComponent(jButtonHuy, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jTextFieldHeSo)
                                            .addComponent(jLabelGiaBan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel11))))
                            .addComponent(jLabel6)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 469, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabelTenSP))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jLabelGiaNhap))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(jLabelGiaBan))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jLabel9)
                            .addComponent(jTextFieldHeSo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonApdung)
                            .addComponent(jButtonHuy)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(170, 170, 170))
        );

        jButtonTimKiem.setText("Tìm kiếm");
        jButtonTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTimKiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(238, 238, 238)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelNgayBD, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jComboBoxCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(88, 88, 88)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelNgayKT))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(70, 70, 70)
                                .addComponent(jComboBoxLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(260, 260, 260)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabelTenCTKM)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelTenCTKM)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabelNgayBD)
                    .addComponent(jLabelNgayKT))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonTimKiem))
                .addGap(43, 43, 43)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTimKiemActionPerformed
        // TODO add your handling code here:
      //  tbModel = null;
        int indexLoai= this.jComboBoxLoaiSP.getSelectedIndex();
        maLoaiSP = (Integer)this.vtMaLoaiSP.get(indexLoai);
        Loaisp objLoaiNV = LoaiSanPhamDao.layThongTinLoaisp(maLoaiSP);
        
        int indexCH = this.jComboBoxCuaHang.getSelectedIndex();
        maCH = (Integer)this.vtMaLoaiCH.get(indexCH);
        Cuahang objCuahang = CuaHangDAO.GetCuahangID(maCH);
        ls = SanPhamDao.getListSanPham(maCH, maLoaiSP);        
        this.tbModel = new DefaultTableModel(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; 
        }
        };
        this.tbModel.addColumn("STT");        
        this.tbModel.addColumn("Tên sản phẩm");                
        this.tbModel.addColumn("Đơn giá nhập");  
        this.tbModel.addColumn("Đơn giá bán");  
        this.tbModel.addColumn("Số lượng tồn");                                
        this.tbModel.addColumn("Ghi chú");                          
        for(int i=0;i<ls.size();i++)
        {
            Vector vt = new Vector();            
            vt.add(i+1);
            vt.add(ls.get(i).getTensp());
            vt.add(ls.get(i).getDongianhap());            
            vt.add(ls.get(i).getSoluongton());
            vt.add(ls.get(i).getGhichu());            
            tbModel.addRow(vt);
        }        
        this.jTableDanhSachSP.setModel(tbModel);
        
    }//GEN-LAST:event_jButtonTimKiemActionPerformed

    private void jButtonHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHuyActionPerformed
        // TODO add your handling code here:
        int dong = this.jTableCTKM.getSelectedRow();
        if (dong < 0) {
            JOptionPane.showMessageDialog(this, "Chọn sản phẩm để hủy áp dụng");
            return;
        }
        int idSP = lsChitietKM.get(dong).getChitietsanpham().getIdctsp();
        Chitietchuongtrinhkm objCTCTKM =null;
        objCTCTKM = ChiTietChuongTrinhKhuyenMaiDAO.GetChitietchuongtrinhkmSP(JInternalFrameChuongTrinhKhuyenMai.getIDCTKM(), idSP);
        Chitietsanpham objCTSP = new Chitietsanpham();
        objCTSP = ChiTietSanPhamDAO.getChiTietSanPhamByID(idSP);
        if (objCTCTKM!=null){            
            objCTCTKM.setTinhtrang(0);
            objCTSP.setChitietchuongtrinhkm(null);
            if(ChiTietChuongTrinhKhuyenMaiDAO.UpdateChitietchuongtrinhkm(objCTCTKM)&& ChiTietSanPhamDAO.UpdateChitietSP(objCTSP))
            {                
                JOptionPane.showMessageDialog(this, "Hủy thành công");
                lsChitietKM.remove(dong);
                tbModelKM.removeRow(dong);            
                    ls.remove(dong);
                    for(int i = 0;i<this.tbModelKM.getRowCount();i++)
                    {                
                        this.tbModelKM.setValueAt(i+1, i, 0);
                    }
                    this.jTableCTKM.setModel(tbModelKM);
            }            
        }                
        else
        {
            JOptionPane.showMessageDialog(this, "Hủy thất bại");
        }
    }//GEN-LAST:event_jButtonHuyActionPerformed

    private void jButtonApdungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApdungActionPerformed
        // TODO add your handling code here:
        Chitietchuongtrinhkm objChitietKM = new Chitietchuongtrinhkm();
        float heSo = Float.parseFloat(this.jTextFieldHeSo.getText());
        heSo = heSo / 100;
        if (heSo > 1) {
            JOptionPane.showMessageDialog(this, "Muc giam gia khong duoc qua 100%");
            return;
        }
        objChitietKM.setHesogiamgia(heSo);
        Chuongtrinhkhuyenmai objCTKM = new Chuongtrinhkhuyenmai();
        objCTKM = ChuongTrinhKhuyenMaiDAO.GetChuongtrinhkhuyenmaiID(JInternalFrameChuongTrinhKhuyenMai.getIDCTKM());
        objChitietKM.setChuongtrinhkhuyenmai(objCTKM);
        objChitietKM.setTinhtrang(1);
        //get idsp, idch
        int indexLoai = this.jComboBoxCuaHang.getSelectedIndex();
        int iMaCH = (Integer) this.vtMaLoaiCH.get(indexLoai);
        int dong = this.jTableDanhSachSP.getSelectedRow();
        if (dong < 0) {
            JOptionPane.showMessageDialog(this, "Chọn sản phẩm để áp dụng chương trình");
            return;
        }
        //   int indexLoaiSP = this.jComboBoxLoaiSP.getSelectedIndex();
        //  int iMaLoai = (Integer)this.vtMaLoaiCH.get(indexLoaiSP);
        //Loaisp objLoaiNV = LoaiSanPhamDao.layThongTinLoaisp(iMaLoai);                
        //ls = SanPhamDao.getListSanPham(iMaCH, iMaLoai);                   
        int idSP = ls.get(dong).getIdsp();
        Chitietchuongtrinhkm objCTCTKM =null;
        ChiTietChuongTrinhKhuyenMaiDAO.GetChitietchuongtrinhkmSP(JInternalFrameChuongTrinhKhuyenMai.getIDCTKM(), idSP);
        if (objCTCTKM!=null){
            objCTCTKM.setHesogiamgia(heSo);
            if(ChiTietChuongTrinhKhuyenMaiDAO.UpdateChitietchuongtrinhkm(objCTCTKM))
                JOptionPane.showMessageDialog(this, "Cập nhật thành công");
        }
        else{
            Chitietsanpham objChitietsanpham = new Chitietsanpham();
            objChitietsanpham = ChiTietSanPhamDAO.getChiTietSanPham(maCH, idSP);
            objChitietsanpham.setChitietchuongtrinhkm(objChitietKM);
            float giaKM = 0;
            if (this.jLabelGiaBan.getText().isEmpty() == false) {
                giaKM = objChitietsanpham.getGiaban() - (objChitietsanpham.getGiaban() * heSo);
            } else {
                giaKM = Float.parseFloat(this.jLabelGiaNhap.getText()) - (Float.parseFloat(this.jLabelGiaNhap.getText()) * heSo);
            }
            objChitietKM.setChitietsanpham(objChitietsanpham);
            objChitietsanpham.setGiaban(giaKM);
            if (ChiTietChuongTrinhKhuyenMaiDAO.InsertChitietchuongtrinhkm(objChitietKM) && ChiTietSanPhamDAO.UpdateChitietSP(objChitietsanpham)) {
                JOptionPane.showMessageDialog(this, "Áp dụng thành công");
            }
            else{
                JOptionPane.showMessageDialog(this, "Áp dụng thất bại");
            }
        }
    }//GEN-LAST:event_jButtonApdungActionPerformed

    private void jTableDanhSachSPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDanhSachSPMouseClicked
        // TODO add your handling code here:
        this.jLabelTenSP.setText(null);
        this.jLabelGiaNhap.setText(null);
        this.jTextFieldHeSo.setText("");
        try {
            int dong = this.jTableDanhSachSP.getSelectedRow();
            if (dong >= 0) {
                String tenSP = "";
                String giaBan = "";

                if (this.jTableDanhSachSP.getValueAt(dong, 1).toString().isEmpty() == false) {
                    tenSP = this.jTableDanhSachSP.getValueAt(dong, 1).toString();
                    this.jLabelTenSP.setText(tenSP);
                }

                if (this.jTableDanhSachSP.getValueAt(dong, 2).toString().isEmpty() == false) {
                    giaBan = this.jTableDanhSachSP.getValueAt(dong, 2).toString();
                    this.jLabelGiaNhap.setText(giaBan);
                }
            }
            int idSP = ls.get(dong).getIdsp();
            Chitietsanpham objChitietsanpham = new Chitietsanpham();
            objChitietsanpham = ChiTietSanPhamDAO.getChiTietSanPham(maCH, idSP);
            this.jLabelGiaBan.setText(objChitietsanpham.getGiaban().toString());
        } catch (Exception ex) {
            //throw ex;
        }
    }//GEN-LAST:event_jTableDanhSachSPMouseClicked

    private void jTableCTKMMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableCTKMMouseClicked
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTableCTKMMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDialogChiTietChuongTrinhKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDialogChiTietChuongTrinhKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDialogChiTietChuongTrinhKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDialogChiTietChuongTrinhKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JDialogChiTietChuongTrinhKM dialog = new JDialogChiTietChuongTrinhKM(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
                  
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonApdung;
    private javax.swing.JButton jButtonHuy;
    private javax.swing.JButton jButtonTimKiem;
    private javax.swing.JComboBox jComboBoxCuaHang;
    private javax.swing.JComboBox jComboBoxLoaiSP;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelGiaBan;
    private javax.swing.JLabel jLabelGiaNhap;
    private javax.swing.JLabel jLabelNgayBD;
    private javax.swing.JLabel jLabelNgayKT;
    private javax.swing.JLabel jLabelTenCTKM;
    private javax.swing.JLabel jLabelTenSP;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableCTKM;
    private javax.swing.JTable jTableDanhSachSP;
    private javax.swing.JTextField jTextFieldHeSo;
    // End of variables declaration//GEN-END:variables
}
